package com.bs.web.consumer.service;

import com.bs.web.consumer.dao.UserRepository;
import com.bs.web.consumer.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

/**
 * Created by Nominal on 2018/3/6 0006.
 * 微博：@Mr丶Li_Anonym
 *
 *
 * 重写UserDetailsService接口，
 * 然后实现该接口中的loadUserByUsername方法，通过该方法查询到对应的用户，
 * 这里之所以要实现UserDetailsService接口，
 * 是因为在Spring Security中我们配置相关参数需要UserDetailsService类型的数据。
 */
public class CustomUserService implements UserDetailsService {

    @Autowired
    UserRepository userRepository;
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        User user = userRepository.findByUsername(s);
        if (user == null) {
            throw new UsernameNotFoundException("用户名不存在");
        }
        System.out.println("s:"+s);
        System.out.println("username:"+user.getUsername()+";password:"+user.getPassword());
        return user;
    }
}
